import time

from selenium.webdriver.common.by import By
from common.Utils import BlogDriver

#测试博客登陆页面

class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/login"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

#成功登陆的测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("admin1")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        time.sleep(2)

        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()
        time.sleep(2)

        #能够找到博客首页用户的昵称,说明登陆成功,否则登陆失败
        self.driver.find_element(By.XPATH,"//span[text()='admin1' or contains(text(), '用户')]")
        #添加屏幕截图
        BlogDriver.getScreenShot()

        time.sleep(2)

        #返回到登陆页面
        self.driver.back()

#异常登陆的测试用例
    def LoginFailTest(self):
        time.sleep(2)

        #若连续多次的send_keys则会出现关键词拼接,而不是替换,若要替换需要先clear
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()

        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("admin")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        time.sleep(2)

        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()
        time.sleep(2)

        #检查是否登陆失败
        actual = self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div").text
        #添加屏幕截图
        BlogDriver.getScreenShot()
        #断言检测一下是否符合预期
        assert actual == "错误的用户名和密码"


login = BlogLogin()
login.LoginSucTest()
login.LoginFailTest()